import os
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtGui import QIcon, QFont, QTextCharFormat, QSyntaxHighlighter, QColor
from PyQt5.QtCore import QSize, QRegExp

# 自定义带图标和提示的按钮
class IconButton(QPushButton):
    def __init__(self, icon_path, tooltip, parent=None):
        super().__init__(parent)
        self.setFixedSize(30, 30)
        
        # 处理相对路径
        if not os.path.isabs(icon_path):
            # 如果是相对路径，则相对于当前模块所在目录
            current_dir = os.path.dirname(os.path.abspath(__file__))
            icon_path = os.path.join(current_dir, icon_path)
        
        # 检查图标路径是否存在，并加载图标
        if not os.path.exists(icon_path):
            print(f"警告: 图标文件不存在 {icon_path}")
            
        icon = QIcon(icon_path)
        if icon.isNull():
            print(f"警告: 无法加载图标 {icon_path}")
        
        self.setIcon(icon)
        self.setIconSize(QSize(20, 20))
        self.setToolTip(tooltip)
        self.setStyleSheet("""
            QPushButton {
                background-color: #FFFFFF;
                border-radius: 15px;
                border: 1px solid #2196F3;
            }
            QPushButton:hover {
                background-color: rgba(33, 150, 243, 0.1);
            }
            QPushButton:disabled {
                background-color: transparent;
                border: 1px solid #BBDEFB;
            }
            QToolTip {
                background-color: #FFFFFF;
                color: #333333;
                border: 1px solid #2196F3;
            }
        """)

# 自定义文本高亮器（用于总结对话框）
class MarkdownHighlighter(QSyntaxHighlighter):
    def __init__(self, document):
        super().__init__(document)
        
        # 标题规则
        self.heading_rules = []
        colors = [
            QColor("#FF7043"), # H1 - 橙色
            QColor("#F9AA33"), # H2 - 黄色
            QColor("#64B5F6"), # H3 - 蓝色
            QColor("#81C784"), # H4 - 绿色
        ]
        
        for i, color in enumerate(colors, start=1):
            pattern = QRegExp(f"^#{{{i}}}[^#].*")
            self.heading_rules.append((pattern, color))
        
        # 粗体规则
        bold_format = QTextCharFormat()
        bold_format.setFontWeight(QFont.Bold)
        bold_format.setForeground(QColor("#4FC3F7"))
        self.bold_rule = (QRegExp("\\*\\*[^\\*]+\\*\\*"), bold_format)
        
        # 列表规则
        list_format = QTextCharFormat()
        list_format.setForeground(QColor("#AED581"))
        self.list_rule = (QRegExp("^(\\s*[-*]\\s|\\d+\\.\\s)"), list_format)
        
        # 代码块规则
        code_format = QTextCharFormat()
        code_format.setBackground(QColor("#263238"))
        code_format.setFontFamily("Monospace")
        code_format.setForeground(QColor("#E0E0E0"))
        self.code_rule = (QRegExp("^```.*$"), code_format)
        
        # 引用规则
        quote_format = QTextCharFormat()
        quote_format.setForeground(QColor("#FFCC80"))
        self.quote_rule = (QRegExp("^>.*$"), quote_format)
        
        # 链接规则
        link_format = QTextCharFormat()
        link_format.setForeground(QColor("#29B6F6"))
        link_format.setFontUnderline(True)
        self.link_rule = (QRegExp("\\$([^\\$]+)\\]\\([^\\)]+\\)"), link_format)

    def highlightBlock(self, text):
        # 应用标题规则
        for pattern, color in self.heading_rules:
            expression = QRegExp(pattern)
            index = expression.indexIn(text)
            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, color)
                font = QFont()
                font.setPointSize(20 - len(pattern.pattern()) * 2)
                self.setFormat(index, length, font)
                index = expression.indexIn(text, index + length)
        
        # 应用粗体规则
        expression, format = self.bold_rule
        index = expression.indexIn(text)
        while index >= 0:
            length = expression.matchedLength()
            # 从匹配中删除星号
            self.setFormat(index + 2, length - 4, format)
            index = expression.indexIn(text, index + length)
        
        # 应用列表规则
        expression, format = self.list_rule
        index = expression.indexIn(text)
        while index >= 0:
            length = expression.matchedLength()
            self.setFormat(index, length, format)
            index = expression.indexIn(text, index + length)
        
        # 应用代码块规则
        expression, format = self.code_rule
        if expression.indexIn(text) >= 0:
            self.setFormat(0, len(text), format)
        
        # 应用引用规则
        expression, format = self.quote_rule
        if expression.indexIn(text) >= 0:
            self.setFormat(0, len(text), format)
        
        # 应用链接规则
        expression, format = self.link_rule
        index = expression.indexIn(text)
        while index >= 0:
            length = expression.matchedLength()
            self.setFormat(index, length, format)
            index = expression.indexIn(text, index + length)